<template>
  <tbc-dialog ref="dialog" title="发放优惠券" @confirm="formSubmit">
    <el-form class="compact" label-position="top">
      <el-form-item label="发放用户">
        <el-input type="textarea" rows="6" v-model="userIdList" placeholder="用户ID，多个逗号分隔"></el-input>
      </el-form-item>
    </el-form>
  </tbc-dialog>
</template>
<script lang="ts">
import {defineComponent, ref, Ref} from 'vue'
import http from '@/plugin/http'
import {ElMessage} from "element-plus";

export default defineComponent({

  name: 'coupon-send',

  setup() {
    let coupon = {coupon_id: ''}
    let userIdList: Ref<string> = ref('')

    const dialog: Ref = ref(null)
    const show = (data) => {
      coupon = data
      userIdList.value = ''
      dialog.value.show()
    }

    const formSubmit = async () => {
      const res = await http.post('/coupon/send', {
        coupon_id: coupon.coupon_id,
        user_id: userIdList.value
      })
      if (res.data.code !== 0) return ElMessage.error(res.data.message)
      ElMessage.success(res.data.message)
    }

    return {
      dialog,
      show,
      userIdList,
      formSubmit
    }
  }


  //
  // methods: {
  //
  //
  //
  //   async loadDataList(page, size) {
  //     this.loading = true;
  //     if (page) this.page = page;
  //     if (size) this.size = size;
  //     const res = await this.http.get('/coupon/user', {
  //       coupon_id: this.coupon.coupon_id,
  //       page: this.page,
  //       size: this.size,
  //       keyword: this.keyword,
  //       status: this.status,
  //     })
  //     this.loading = false;
  //     if (res.data.code !== 0) return this.$message.error(res.data.message);
  //     this.total = res.data.data.total;
  //     this.dataList = res.data.data.list;
  //   },
  //
  //   sendToAllUser() {
  //     this.$ui.warning("是否对全部用户进行发放？").then(async () => {
  //       await this._doSendRequest({all: 1}, (res) => {
  //         if (res.data.code !== 0) return this.$message.error(res.data.message);
  //       })
  //     })
  //   },
  //
  //   sendToOneUser(data) {
  //     this._doSendRequest({user_id: data.user_id}, (res) => {
  //       if (res.data.code !== 0) return this.$message.error(res.data.message);
  //       this.$message.success(res.data.message)
  //       data.received_count += 1
  //     })
  //   },
  //
  //   async _doSendRequest(data, callback) {
  //     const form = {...data, coupon_id: this.$route.query.id}
  //     const res = await this.http.post('/coupon/send', form)
  //     callback && callback(res)
  //   }
  // },
})
</script>